<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of grid</title>
  <meta name="keywords" content="grid">
  <meta name="description" content="grid(g,a,b) --- create an a-by-b grid graph">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="../index.html">matgraph</a> &gt; <a href="index.html">@graph</a> &gt; grid.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for matgraph/@graph&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>grid
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>grid(g,a,b) --- create an a-by-b grid graph</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function grid(g,a,b) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> grid(g,a,b) --- create an a-by-b grid graph
 grid(g,a) is the same as grid(g,a,a)</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="add.html" class="code" title="function add(g,i,j)">add</a>	add --- add edge(s) to the graph</li><li><a href="embed.html" class="code" title="function embed(g,xy)">embed</a>	embed --- create an embedding for a graph</li><li><a href="resize.html" class="code" title="function resize(g, n)">resize</a>	resize(g,n) --- change the number of vertices in g to n</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->

<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="#_sub1" class="code">function v = ij2v(i,j,a,b)</a></li></ul>
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function grid(g,a,b)</a>
0002 <span class="comment">% grid(g,a,b) --- create an a-by-b grid graph</span>
0003 <span class="comment">% grid(g,a) is the same as grid(g,a,a)</span>
0004 
0005 
0006 
0007 <span class="keyword">if</span> nargin==2
0008     b = a;
0009 <span class="keyword">end</span>
0010 
0011 n = a*b;
0012 
0013 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,0);
0014 <a href="resize.html" class="code" title="function resize(g, n)">resize</a>(g,n);
0015 
0016 <span class="keyword">for</span> i=1:a      <span class="comment">% row index</span>
0017     <span class="keyword">for</span> j=1:b  <span class="comment">% col index</span>
0018         v = <a href="#_sub1" class="code" title="subfunction v = ij2v(i,j,a,b)">ij2v</a>(i,j,a,b);
0019         
0020         <span class="comment">% left</span>
0021         <span class="keyword">if</span> (i&gt;1)
0022             w = <a href="#_sub1" class="code" title="subfunction v = ij2v(i,j,a,b)">ij2v</a>(i-1,j,a,b);
0023             <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,v,w);
0024         <span class="keyword">end</span>
0025         
0026         <span class="comment">% right</span>
0027         <span class="keyword">if</span> (i&lt;a)
0028             w = <a href="#_sub1" class="code" title="subfunction v = ij2v(i,j,a,b)">ij2v</a>(i+1,j,a,b);
0029             <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,v,w);
0030         <span class="keyword">end</span>
0031         
0032         <span class="comment">% down</span>
0033         <span class="keyword">if</span> (j&gt;1)
0034             w = <a href="#_sub1" class="code" title="subfunction v = ij2v(i,j,a,b)">ij2v</a>(i,j-1,a,b);
0035             <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,v,w);
0036         <span class="keyword">end</span>
0037         
0038         <span class="comment">% up</span>
0039         <span class="keyword">if</span> (j&lt;b)
0040             w = <a href="#_sub1" class="code" title="subfunction v = ij2v(i,j,a,b)">ij2v</a>(i,j+1,a,b);
0041             <a href="add.html" class="code" title="function add(g,i,j)">add</a>(g,v,w);
0042         <span class="keyword">end</span>
0043     <span class="keyword">end</span>
0044 <span class="keyword">end</span>
0045 
0046 
0047 <span class="comment">% now create the embedding</span>
0048 
0049 xy = zeros(n,2);
0050 
0051 <span class="keyword">for</span> i=1:a
0052     <span class="keyword">for</span> j=1:b
0053         v = <a href="#_sub1" class="code" title="subfunction v = ij2v(i,j,a,b)">ij2v</a>(i,j,a,b);
0054         xy(v,:) = [j,i];
0055     <span class="keyword">end</span>
0056 <span class="keyword">end</span>
0057 <a href="embed.html" class="code" title="function embed(g,xy)">embed</a>(g,xy)
0058 
0059 
0060 
0061 
0062 <span class="comment">% this is a helper function that converts i,j grid coordinates to a vertex</span>
0063 <span class="comment">% number (1,1) --&gt; 1 up to (a,b) --&gt; ab</span>
0064 
0065 <a name="_sub1" href="#_subfunctions" class="code">function v = ij2v(i,j,a,b)</a>
0066 
0067 v = i + a*(j-1);</pre></div>
<hr><address>Generated on Fri 30-Apr-2010 07:51:16 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>